<?php

abstract class Rapide_Controller implements Rapide_Controller_Interface
{
	private
		$_aParameters = array(),
		$_oConfig,
		$_oUser,
		$_oNextToken,
		$_oLanguage;

	public function __construct()
	{
		$this->_oConfig = new Rapide_Config;
		$this->_oConfig->load('Controller.php');

		try
		{
			$this->_oConfig->load(_DIR_CONFIGURATIONS_CONTROLLERS . '/' . substr(get_class($this), 11) . '.php');
		}
		catch(Rapide_Config_Loader_PhpArray_Exception $oException)
		{
		}
	}

	public function initialize(array $aParemeters)
	{
		$this->_aParameters = $aParemeters;
	}

	public function getParameter($sParameter)
	{
		return isset($this->_aParameters[$sParameter]) ?
					 $this->_aParameters[$sParameter] :
					 null;
	}

	public function hasParameter($sParameter)
	{
		return isset($this->_aParameters[$sParameter]);
	}

	public function forward($sController = null, $sAction = null, array $aParameters = array())
	{
		if(empty($sController))
		{
			$sController = $this->_oConfig->get('Controller', 'default', 'controller');
		}

		if(empty($sAction))
		{
			$sAction = $this->_oConfig->get('Controller', 'default', 'action');
		}

		$this->_oNextToken = new Rapide_Dispatcher_Token($sController, $sAction, $aParameters);
	}

	public function redirect($sController = null, $sAction = null, array $aParameters = array(), $sScheme = null)
	{
		if(headers_sent())
		{
			$sError = 'Headers already sent';
			throw new Rapide_Controller_Exception($sError);
		}

		$sUrl = Rapide_Controller_Front::getInstance()
						->getRouter()
						->url($sController, $sAction, $aParameters, $sScheme);

		header('Location: ' . $sUrl);
	}

	public function getNextToken()
	{
		return $this->_oNextToken instanceof Rapide_Dispatcher_Token_Interface ?
					 $this->_oNextToken :
					 null;
	}

	public function getConfig()
	{
		$sControllerName = substr(get_class($this), 11);

		return $this->_oConfig->get(_DIR_CONFIGURATIONS_CONTROLLERS, $sControllerName);
	}

	public function getLang()
	{
		if(!$this->_oLanguage instanceof Rapide_Language)
		$this->_oLanguage = Rapide_Language::getInstance();

		return $this->_oLanguage;
	}

	public function IndexAction()
	{
		$this->forward();
	}
}

?>